package top.yaofengqiao.springcloudsimple.system.biz.infrastructure.repository;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository;
import top.yaofengqiao.springcloudsimple.common.util.PageInfo;
import top.yaofengqiao.springcloudsimple.common.util.PageParam;
import top.yaofengqiao.springcloudsimple.system.biz.domain.dict.entity.DictEntity;
import top.yaofengqiao.springcloudsimple.system.biz.domain.dict.repository.IDictRepository;
import top.yaofengqiao.springcloudsimple.system.biz.infrastructure.dao.DictDataMapper;
import top.yaofengqiao.springcloudsimple.system.biz.infrastructure.dao.DictTypeMapper;
import top.yaofengqiao.springcloudsimple.system.biz.infrastructure.po.DictDataPO;
import top.yaofengqiao.springcloudsimple.system.biz.infrastructure.po.DictTypePO;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yfq
 * @date 10:38
 * @description
 */
@Repository
public class DictRepository implements IDictRepository {
    @Resource
    private DictTypeMapper dictTypeMapper;
    @Resource
    private DictDataMapper dictDataMapper;

    @Override
    public PageInfo<DictEntity> queryDictTypes(DictEntity dictEntity, PageParam pageParam) {
        Page<DictTypePO> page = dictTypeMapper.selectPage(new Page<>(pageParam.getPage(), pageParam.getSize()),
                Wrappers.<DictTypePO>lambdaQuery()
                        .eq(CharSequenceUtil.isNotEmpty(dictEntity.getType()), DictTypePO::getType, dictEntity.getType())
                        .eq(CharSequenceUtil.isNotEmpty(dictEntity.getName()), DictTypePO::getName, dictEntity.getName()));

        return PageInfo.build(page).convert(o -> DictEntity.builder()
                .id(o.getId())
                .name(o.getName())
                .type(o.getType())
                .status(o.getStatus())
                .remark(o.getRemark())
                .build());
    }

    @Override
    public DictEntity queryDictEntityById(long id) {
        DictTypePO dictTypePO = dictTypeMapper.selectById(id);

        return buildDictEntity(dictTypePO);
    }

    @Override
    public DictEntity queryDictEntityByType(String type) {
        DictTypePO dictTypePO = dictTypeMapper.selectOne(Wrappers.<DictTypePO>lambdaQuery().eq(DictTypePO::getType, type));

        return buildDictEntity(dictTypePO);
    }

    @Override
    public boolean updateDictType(DictEntity dictEntity) {
        DictTypePO dictTypePO = buildDictTypePO(dictEntity);

        return dictTypeMapper.updateById(dictTypePO) == 1;
    }

    @Override
    public boolean updateDictData(DictEntity.Value value) {
        DictDataPO dictDataPO = buildDictDataPO(value);

        return dictDataMapper.updateById(dictDataPO) == 1;
    }

    @Override
    public boolean saveDictType(DictEntity dictEntity) {
        DictTypePO dictTypePO = buildDictTypePO(dictEntity);

        return dictTypeMapper.updateById(dictTypePO) == 1;
    }

    @Override
    public boolean saveDictData(DictEntity.Value value) {
        DictDataPO dictDataPO = buildDictDataPO(value);

        return dictDataMapper.updateById(dictDataPO) == 1;
    }

    private DictEntity buildDictEntity(DictTypePO dictTypePO) {
        if (dictTypePO == null) return null;

        String type = dictTypePO.getType();
        List<DictDataPO> dictDataPOS = dictDataMapper.selectList(Wrappers.<DictDataPO>lambdaQuery().eq(DictDataPO::getDictType, type));

        return DictEntity.builder()
                .name(dictTypePO.getName())
                .type(dictTypePO.getType())
                .status(dictTypePO.getStatus())
                .remark(dictTypePO.getRemark())
                .values(dictDataPOS.stream()
                        .map(o -> DictEntity.Value.builder()
                                .id(o.getId())
                                .sort(o.getSort())
                                .label(o.getLabel())
                                .value(o.getValue())
                                .dictType(o.getDictType())
                                .status(o.getStatus())
                                .colorType(o.getColorType())
                                .cssClass(o.getCssClass())
                                .remark(o.getRemark())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    private DictTypePO buildDictTypePO(DictEntity dictEntity) {
        if (dictEntity == null) return null;

        DictTypePO dictTypePO = new DictTypePO();
        dictTypePO.setId(dictEntity.getId());
        dictTypePO.setName(dictEntity.getName());
        dictTypePO.setType(dictEntity.getType());
        dictTypePO.setStatus(dictEntity.getStatus());
        dictTypePO.setRemark(dictEntity.getRemark());
        dictTypePO.setUpdateTime(LocalDateTime.now());

        return dictTypePO;
    }

    private DictDataPO buildDictDataPO(DictEntity.Value value) {
        if (value == null) return null;

        DictDataPO dictDataPO = new DictDataPO();
        dictDataPO.setId(value.getId());
        dictDataPO.setSort(value.getSort());
        dictDataPO.setLabel(value.getLabel());
        dictDataPO.setValue(value.getValue());
        dictDataPO.setDictType(value.getDictType());
        dictDataPO.setStatus(value.getStatus());
        dictDataPO.setColorType(value.getColorType());
        dictDataPO.setCssClass(value.getCssClass());
        dictDataPO.setRemark(value.getRemark());

        return dictDataPO;
    }

}
